

/*
 * @Author: jiejie
 * @Github: https://github.com/jiejieTop
 * @Date: 2019-12-11 21:53:07
 * @LastEditTime : 2022-06-12 17:48:06
 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
 */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pthread.h>
#include <common.h>
#include "../include/input_manager.h"
#include "mqtt_config.h"
#include "mqtt_log.h"
#include "mqttclient.h"
#include "mqtt_manager.h"

// #define TEST_USEING_TLS  
// extern const char *test_ca_get();
char *name_string = "yangning";
char *password_string = "123456";
mqtt_client_t *client = NULL;
#define control_commond_number 2
char* control_commond_strings[] = {
    "led:on",
    "led:off",
};
static PInputEvent ptInputEvent;

static void topic1_handler(void* client, message_data_t* msg)
{
    (void) client;
    // int i;
    MQTT_LOG_I("-----------------------------------------------------------------------------------");
    MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload);
    MQTT_LOG_I("-----------------------------------------------------------------------------------");
    sleep(3);
   
    ptInputEvent = malloc(sizeof(PInputEvent));
    if(NULL == ptInputEvent)return -1;
    ptInputEvent->iType = INPUT_TYPE_NET;
    memcpy(ptInputEvent->str, (char*)msg->message->payload, strlen((char*)msg->message->payload));
    MQTT_LOG_I("%s:%d %s()behind pthread_mutex_lock\n", __FILE__, __LINE__, __FUNCTION__);
    sleep(5);
	
    pthread_mutex_lock(&g_inputMutex);
	PutInputEventToBuffer(ptInputEvent);

	/* 唤醒等待数据的线程 */
	pthread_cond_signal(&g_inputConVar); /* 通知接收线程 */
	pthread_mutex_unlock(&g_inputMutex);
  
    printf("\nmqtt_sub topic1_handler...\n");
  
  
  
    //负载数据对应设备和命令 格式如 设备名称:命令（light:on  light:off） 考虑通过结构体链表实现控制操作设备
    // for (i = 0; i < control_commond_number; i++)
    // {
    //     if(!strcmp(msg->message->payload,control_commond_strings[i]));
    // }
    // ptItemCfg = GetItemCfgByName(ptButton->name);
	// if (ptItemCfg->command[0] != '\0')
	// {
	// 	sprintf(command, "%s %s", ptItemCfg->command, command_status[command_status_index]);
	// 	system(command);
	// }
    
    
}

void *mqtt_publish_thread(void *arg)
{
    mqtt_client_t *client = (mqtt_client_t *)arg;
	int cnt = 0;

    char buf[100] = { 0 };
    mqtt_message_t msg;
    memset(&msg, 0, sizeof(msg));
    sprintf(buf, "welcome to mqttclient, this is a publish test...");
    
    sleep(2);

    mqtt_list_subscribe_topic(client);

    msg.payload = (void *) buf;
    
    while(1) {
        //sprintf(buf, "welcome to mqttclient, this is a publish test, a rand number: %d ...", random_number());
        sprintf(buf, "arm-linux, %d", cnt++);

        msg.qos = 0;
        mqtt_publish(client, "yangningarmtopic", &msg);
        printf("\nmqtt_publish...\n");
        sleep(5);
    }
}

int mqtt_init(void)
{
    int res;
    pthread_t thread1;  
    printf("\nwelcome to mqttclient test...\n");

    mqtt_log_init();

    client = mqtt_lease();

#ifdef TEST_USEING_TLS
    mqtt_set_port(client, "8883");
    mqtt_set_ca(client, (char*)test_ca_get());
#else
    mqtt_set_port(client, "1883");
#endif


    mqtt_set_host(client, "192.168.5.11"); /* linux调试专用IP */
    mqtt_set_client_id(client, random_string(10));  //设置客户端id
    mqtt_set_user_name(client, name_string);  //设置客户端的用户名
    mqtt_set_password(client, password_string);   //设置客户端的密码
    mqtt_set_clean_session(client, 1);             //设置在断开连接后清除会话

    mqtt_connect(client);
    
    mqtt_subscribe(client, "control:command", QOS0, topic1_handler); 
    mqtt_subscribe(client, "topic2", QOS1, NULL);
    mqtt_subscribe(client, "topic3", QOS2, NULL);
    sleep(5);
    // res = pthread_create(&thread1, NULL, mqtt_publish_thread, client);
    // if(res != 0) {
    //     MQTT_LOG_E("create mqtt publish thread fail");
    //     exit(res);
    // }
    return 1;
}